package year2023.bitwise;

public class Code02_EvenTimesOddTimes {

    //找出数组中出现奇数个次数的数
    public static void printOddTimesNum1(int[] arr){
        int eor = 0;
        for (int i = 0; i < arr.length; i++) {
            eor ^= arr[i];
        }
        System.out.println(eor);
    }

    //找出数组中出现两种出现奇数个次数的数
    public static void printOddTimesNum2(int[] arr){
        int eor = 0;
        for (int i = 0; i < arr.length; i++) {
            eor ^= arr[i];
        }
        // eor = a ^ b;
        // eor != 0
        // eor必然有一个位置上是1
        int right0ne = eor & (-eor);//提取最右侧的1
        int onlyOne = 0; // eor'
        for (int i = 0; i < arr.length; i++) {
           //  arr[1] = 111100011110000
           // rightOne= 000000000010000
           if ((arr[i] & right0ne) != 0){
               onlyOne ^= arr[i];
           }
        }
        System.out.println(onlyOne + " " + (eor ^ onlyOne));
    }

    public static void main(String[] args) {
        int[] arr = {1,1,2,2,3,4,4,4};
        printOddTimesNum2(arr);
    }

}
